﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using System.Globalization;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.NeedsAnalysis
{
    public partial class NeedsAnalysissDetails : SOSecurityPage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["nid"]) && !string.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    if (!IsPostBack)
                    {
                        ValidateUserCredentials(Request.Url.LocalPath);
                        int nid = Convert.ToInt32(Request.QueryString["nid"]);
                        string ac = Request.QueryString["a"];

                        CustomerFacade cFac = new CustomerFacade();

                        SalesOne.BusinessObjects.NeedsAnalysis nad = cFac.GetNeedAnalysis(nid);


                        lblProspName.Text = nad.LastName;
                        lblDob.Text = nad.Dob;
                        lblGender.Text = nad.Gender;
                        lblChild.Text = nad.NoOfChild.ToString();
                        lblMarital.Text = nad.Marital;
                        lblIncome.Text = String.Format("{0:0,0.00}", nad.Income);
                        lblExpenses.Text = String.Format("{0:0,0.00}", nad.Expenses);
                        lblAnalysedCover.Text = String.Format("{0:0,0.00}", nad.Analyzes);
                        lblLiability.Text = String.Format("{0:0,0.00}", nad.Liability);
                        lblAssets.Text = String.Format("{0:0,0.00}", nad.Assets); 
                        if (nad.NeedAnOpt == 0)
                        {
                            lblAssets.Text = string.Empty;
                        }
                        else if (nad.NeedAnOpt == 1)
                        {
                            lblAnalysisOpt.Text = "Monthly Income-based Calculation";
                        }
                        else
                        {
                            lblAnalysisOpt.Text = "Monthly Expenditure-based Calculation";
                        }

                        lblTerm.Text = nad.MinTerm.ToString();
                        lblRider.Text = String.Format("{0:0,0.00}", nad.Rider);
                        lblProtectionReq.Text = String.Format("{0:0,0.00}", nad.ProtIn);
                        lblSumAss.Text = String.Format("{0:0,0.00}", nad.Sum);
                        lblCritical.Text = String.Format("{0:0,0.00}", nad.CritAmt);

                        drpStatus.SelectedValue = nad.ApSt.ToString();
                        txtRemarks.Text = nad.Remarks;

                        if (ac == "v")
                        {
                            drpStatus.Enabled = false;
                            txtRemarks.Enabled = false;
                            btnSave.Visible = false;
                        }
                        else if (ac == "u")
                        {
                            drpStatus.Enabled = true;
                            txtRemarks.Enabled = true;
                            btnSave.Visible = true;
                        }
                    }



                }
            }
            catch (Exception)
            {

            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["nid"]) && !string.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    CustomerFacade fac = new CustomerFacade();
                    int nid = Convert.ToInt32(Request.QueryString["nid"]);
                    SalesOne.BusinessObjects.NeedsAnalysis need = fac.GetNeedAnalysis(nid);

                    need.Remarks = txtRemarks.Text;

                    if (drpStatus.SelectedValue != "-1")
                    {
                        need.ApSt = Convert.ToInt32(drpStatus.SelectedValue);
                    }

                    need.CreatedDate = DateTime.Now.ToShortDateString();


                    int s = fac.UpdateNeedAnalysis(need);

                    if (s==1)
                    {
                        MassageSummery("IIS402");
                    }
                    else
                    {
                        MassageSummery("IIS502");
                    }

                }
            }
            catch (Exception)
            {
                MassageSummery("IIS502");
            }
            
            
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["nid"]) && !string.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    int nid = Convert.ToInt32(Request.QueryString["nid"]);
                    string ac = Request.QueryString["a"];

                    CustomerFacade cFac = new CustomerFacade();

                    SalesOne.BusinessObjects.NeedsAnalysis nad = cFac.GetNeedAnalysis(nid);

                    Response.Redirect("~/ContactManagement/ProspectDetails.aspx?id=" + nad.ProspectId + "&t=v&nid=" + nad.NeedAnalysisNo);
                }
            }
            catch (Exception)
            {

            }
            
        }
    }
}
